/**
 * 订单管理初始化
 */
var VpnOrderInfo = {
    id: "VpnOrderInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
VpnOrderInfo.initColumn = function () {
    return [[
            {title: '付款时间', field: 'createTime', visible: true, align: 'center',width: 200},
            {title: '订单号', field: 'orderNo', visible: true, align: 'center',width:200},
            {title: '用户名', field: 'userName', visible: true, align: 'center',width:200},
            // {title: '套餐分类', field: 'packageType', visible: true,
            //     align: 'center', templet:function (item) {
            //         if(item.packageType == '1'){
            //             return "固定"
            //         }
            //         return "活动"
            //     }},
            {title: '套餐名称', field: 'packageName', visible: true, align: 'center'},
            {title: '支付方式', field: 'paymentWay', visible: true,
                align: 'center', templet:function (item) {
                    var value = item.paymentWay;

                    if(value == '1'){
                        return "支付宝"
                    }
                    else if(value == '4'){
                        return "VISA"
                    }
                    else if(value == '3'){
                        return '银联'
                    }
                    else if(value == '5'){
                        return "Master卡"
                    }
                    else if(value == '2'){
                        return "微信支付"
                    }
                    else if(value == '6'){
                        return "IOS内部支付"
                    }
                    else if(value == '7'){
                        return "Google支付"
                    }
                    else if(value == "8"){
                        return "手动添加"
                    }
                    else {
                        return "无"
                    }
                }},
            {title: '订单状态', field: 'status', visible: true,
                align: 'center', templet: function (item) {
                    var value = item.status;

                    if(value == '0'){
                        return '待支付'
                    }
                    else if(value == '1'){
                        return '支付成功'
                    }
                    else if(value == '2'){
                        return '支付失败'
                    }
                }},
            {title: '套餐价格（HKD）', field: 'sellPrice', visible: true,
                align: 'center'},
            {title: '已收金额（HKD）', field: 'money', visible: true,
                align: 'center', templet:function (item) {

                    if (item.status == '1'){
                        return item.money;
                    }
                    else{
                        return "0";
                    }
                }},
        {title: '支付IP', field: 'loginIP',
        visible: true, align: 'center', valign: 'middle',width: 200}
    ]];
};

/**
 * 检查是否选中
 */
VpnOrderInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        VpnOrderInfo.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加订单管理
 */
VpnOrderInfo.openAddVpnOrderInfo = function () {
    var index = layer.open({
        type: 2,
        title: '添加订单管理',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vpnOrderInfo/vpnOrderInfo_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看订单管理详情
 */
VpnOrderInfo.openVpnOrderInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '订单管理详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/vpnOrderInfo/vpnOrderInfo_update/' + VpnOrderInfo.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除订单管理
 */
VpnOrderInfo.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/vpnOrderInfo/delete", function (data) {
            Feng.success("删除成功!");
            VpnOrderInfo.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("vpnOrderInfoId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询订单管理列表
 */
VpnOrderInfo.search = function () {
    var queryData = {};
    queryData['userName'] = $("#userName").val();
    queryData['packageId'] = $("#packageName").val();
    queryData['price'] = $("#price").val();
    queryData['paymentType'] = $("#paymentType").val();
    queryData['orderStatus'] = $("#orderStatus").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();

    VpnOrderInfo.table.reload("tableReload",{
        page: {
            curr: 1 //重新从第1页开始
        },
        where: queryData
    });
};


VpnOrderInfo.initSelectData = function () {
    var ajax = new $ax(Feng.ctxPath + "/vpnOrderInfo/filterInfo", function (data) {

        var packageList = data.packageFilters;
        var paymentTypes = data.paymentTypes;
        packageList.forEach(function (item) {
            $("#packageName").append("'<option value='"+item.pid+"'>"+item.pname+"</option>'");
        }) ;

        if (paymentTypes == null){
            paymentTypes = [
                {paymentId:1},
                {paymentId:2},
                {paymentId:3},
                {paymentId:4},
                {paymentId:5},
                {paymentId:6},
                {paymentId:7},
                {paymentId:8}
            ];
        }

        paymentTypes.forEach(function (item) {
            $("#paymentType").append("'<option value='"+item.paymentId+"'>"+checkPaymentName(item.paymentId)+"</option>'");
        }) ;
    }, function (data) {
        // Feng.error("筛选条件获取失败" + data.responseJSON.message + "!");
    });
    ajax.start();
};

function checkPaymentName(value){
    if(value == 1){
        return "支付宝"
    }
    else if(value == 4){
        return "VISA"
    }
    else if(value == 3){
        return '银联'
    }
    else if(value == 5){
        return "Master卡"
    }
    else if(value == 2){
        return "微信支付"
    }
    else if(value == 6){
        return "IOS内部支付"
    }
    else if(value == 7){
        return "Google支付"
    }
    else if(value == 8){
        return "手动添加"
    }
}


VpnOrderInfo.exportExcl = function () {
    var userName = $("#userName").val();
    var packageId = $("#packageName").val();
    var paymentType = $("#paymentType").val();
    var orderStatus = $("#orderStatus").val();
    var beginTime = $("#beginTime").val();
    var endTime = $("#endTime").val();

    var hrefUrl =
        Feng.ctxPath + "/vpnOrderInfo/download/order?" +
        "userName="+userName+"&packageId="+packageId
        +"&paymentType="+paymentType+"&orderStatus="+orderStatus
        +"&beginTime="+beginTime+"&endTime="+endTime;
    window.location.href = hrefUrl;
};


VpnOrderInfo.tableList = function(queryData){
    layui.use("table",function () {
        var table = layui.table;

        VpnOrderInfo.table = table;

        table.render({
            elem: "#table",
            url: Feng.ctxPath +"/vpnOrderInfo/list",
            cellMinWidth: 120,
            where: queryData,
            page: true,
            limit: 20,
            cols: VpnOrderInfo.initColumn(),
            parseData: function(res){
                return{
                    "code":0,
                    "msg":"获取成功",
                    "count":res.total,
                    "data": res.items
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "tableReload"
        });
    });
};

$(function () {
    // var defaultColunms = VpnOrderInfo.initColumn();
    // var table = new BSTable(VpnOrderInfo.id, "/vpnOrderInfo/list", defaultColunms);
    // table.setPaginationType("client");
    // VpnOrderInfo.table = table.init();

    VpnOrderInfo.tableList();
    VpnOrderInfo.initSelectData();
});
